package com.xayy.mgt.service.impl;

import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.BeanUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.mgt.enums.ProjectItemEnum;
import com.xayy.mgt.mapper.FunctionalIndicatorsMapper;
import com.xayy.mgt.mapper.HistoryInfluenceMapper;
import com.xayy.mgt.mapper.PerformanceIndicatorsMapper;
import com.xayy.mgt.model.FunctionalIndicators;
import com.xayy.mgt.model.HistoryInfluence;
import com.xayy.mgt.model.NodeTreeInfo;
import com.xayy.mgt.model.PerformanceIndicators;
import com.xayy.mgt.model.vo.InfluenceIndexVo;
import com.xayy.mgt.service.HistoryInfluenceService;
import com.xayy.mgt.service.NodeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HistoryInfluenceServiceImpl extends BaseServiceImpl<HistoryInfluence> implements HistoryInfluenceService {
    @Resource
    private HistoryInfluenceMapper historyInfluenceMapper;

    @Resource
    private FunctionalIndicatorsMapper functionalIndicatorsMapper;

    @Resource
    private PerformanceIndicatorsMapper performanceIndicatorsMapper;
    @Autowired
    private NodeInfoService nodeInfoService;

    @Override
    public CheckResult<Object> saveInfluence(HistoryInfluence historyInfluence) {
        CheckResult<HistoryInfluence> init = init(historyInfluence);
        if (init.no())
            return CheckResult.fail(init.getCheckMsg());
        historyInfluenceMapper.insert(init.getData());
        return CheckResult.success("新增成功");
    }

    private CheckResult<HistoryInfluence> init(HistoryInfluence historyInfluence) {
        NodeTreeInfo nodeTreeInfo = nodeInfoService.selectById(historyInfluence.getComponentsId());
        if (BeanUtil.isEmpty(nodeTreeInfo)) {
            return CheckResult.fail("节点信息不存在");
        }
        historyInfluence.setComponentsId(nodeTreeInfo.getId());
        historyInfluence.setComponents(nodeTreeInfo.getNodeName());
        if (historyInfluence.getCategories() == 1) {
            FunctionalIndicators functionalIndicators = functionalIndicatorsMapper.selectOne(FunctionalIndicators
                .builder().nodeId(nodeTreeInfo.getId()).metricsName(historyInfluence.getMetricsName()).build());
            if (BeanUtil.isEmpty(functionalIndicators)) {
                return CheckResult.fail("指标信息不存在");
            }
            historyInfluence.setMetricsCode(functionalIndicators.getMetricsCode());
        } else {
            PerformanceIndicators performanceIndicators = performanceIndicatorsMapper.selectOne(PerformanceIndicators
                .builder().nodeId(nodeTreeInfo.getId()).metricsName(historyInfluence.getMetricsName()).build());
            if (BeanUtil.isEmpty(performanceIndicators)) {
                return CheckResult.fail("指标信息不存在");
            }
            historyInfluence.setMetricsCode(performanceIndicators.getMetricsCode());
        }
        return CheckResult.success(historyInfluence);
    }

    @Override
    public CheckResult<Object> editInfluence(HistoryInfluence historyInfluence) {
        CheckResult<HistoryInfluence> init = init(historyInfluence);
        if (init.no())
            return CheckResult.fail(init.getCheckMsg());
        historyInfluenceMapper.updateByPrimaryKey(init.getData());
        return CheckResult.success("编辑成功");
    }

    @Override
    public CheckResult<Object> deleteInfluence(Long id) {
        historyInfluenceMapper.deleteByPrimaryKey(id);
        return CheckResult.success("删除成功");
    }

    @Override
    public CheckResult<Object> selectInfluence(Long id) {
        Example example = new Example(HistoryInfluence.class);
        example.createCriteria().andEqualTo("faultId", id);
        List<HistoryInfluence> historyInfluences = historyInfluenceMapper.selectByExample(example);
        return CheckResult.success(historyInfluences);
    }

    @Override
    public CheckResult<Object> selectInfluenceIndexs(InfluenceIndexVo influenceIndexVo) {

        // 功能项
        if (influenceIndexVo.getCategories() == ProjectItemEnum.FUNCTIONITEM.getCode()) {
            Example example = new Example(FunctionalIndicators.class);
            example.createCriteria().andEqualTo("nodeId", influenceIndexVo.getComponents());
            List<FunctionalIndicators> functionalIndicatorsList = functionalIndicatorsMapper.selectByExample(example);
            return CheckResult.success(functionalIndicatorsList);
        } else {
            Example example = new Example(PerformanceIndicators.class);
            example.createCriteria().andEqualTo("nodeId", influenceIndexVo.getComponents());
            List<PerformanceIndicators> performanceIndicatorsList =
                performanceIndicatorsMapper.selectByExample(example);
            return CheckResult.success(performanceIndicatorsList);
        }
    }

    @Override
    public CheckResult<Object> selectOneInfluenceIndexDetail(Long id) {
        return CheckResult.success(selectOne(HistoryInfluence.builder().id(id).build()));
    }

}
